package com.xsr.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageInfo;
import com.xsr.pojo.Profile;
import com.xsr.pojo.User;
import com.xsr.service.ProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project peopleThing
 * @date 2022/9/11 17:52
 */
@Controller
@RequestMapping("/profile")
public class ProfileController {
    @Autowired
    private ProfileService profileService;
@RequestMapping("/main")
    public  String  show(Integer pageNum, Integer pageSize,HttpServletRequest request){

    if(pageNum==null){
        pageNum=1;
    }

    if(pageSize==null){
        pageSize=4;
    }
    System.out.println(pageNum+""+pageSize);
    PageInfo<Profile> profilePageInfo = profileService.selectAll(pageNum, pageSize);
request.setAttribute("pageInfo",profilePageInfo);


    return "loginSuccess";
}
@RequestMapping("/detailProfile")
    public  String  detail(Integer id, String method ,Model model){
    Profile profile= profileService.selectById(id);
     model.addAttribute("profile",profile);
    if(method.equals("detail")){

         return "detail";
    }
    else {
        return "update";
    }

}
@RequestMapping("updateProfile")
    public  String  update(HttpServletRequest request, MultipartFile picture){
    try {
        request.setCharacterEncoding("utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String id = request.getParameter("id");
    String name = request.getParameter("name");
    String birthday = request.getParameter("birthday");
    String gender = request.getParameter("gender");
    String career = request.getParameter("career");
    String address = request.getParameter("address");
    String mobile = request.getParameter("mobile");
    String originalFilename = picture.getOriginalFilename();
    String basepath = request.getSession().getServletContext().getRealPath("WEB-INF" + File.separator + "updateload");
    File file = new File(basepath);
    if(!file.exists()){
        file.mkdirs();
    }
    String filename="";
    if(picture!=null&&!picture.getOriginalFilename().equals("")) {



        String substring = UUID.randomUUID().toString().replaceAll("-", "").substring(16);

        String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
        filename= substring+"."+ext;
        try {
            picture.transferTo(new File(basepath+File.separator+filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        Profile profile = null;
        try {
            profile = new Profile(Integer.parseInt(id), name, new SimpleDateFormat("yyyy-MM-dd").parse(birthday), gender, career, address, mobile,filename);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        profileService.update(profile);
    }
    return "forward:main";
}
@RequestMapping("/readImg")
    public  void read(String fileName , HttpServletRequest  request, HttpServletResponse response, ModelAndView modelAndView) throws IOException {
    ServletContext servletContext = request.getServletContext();
   String fileName1 =servletContext.getRealPath("WEB-INF"+ File.separator+"updateload"+File.separator+fileName);
    FileInputStream fileInputStream = new FileInputStream(fileName1);
    byte[] bytes= new byte[1024];
    int len ;
    ServletOutputStream outputStream = response.getOutputStream();
    while((len=fileInputStream.read(bytes))!=-1){
        outputStream.write(bytes,0,len);
    }
    fileInputStream.close();
}
}
